import { store } from "@/store"
import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { showError } from '@/utils/msg-show'
import { getListApi } from '@/api/statistics/userActive'

export const useUserActiveStore = defineStore("UserActive", () => {

    const isZero = ref(true)
    const today = new Date()
    const currentYear = today.getFullYear()
    const currentMonth = today.getMonth() + 1
    const dataList = ref<any[]>([])
    const query = reactive<any>({
        year: currentYear,
        month: currentMonth,
    })

    /** 获取一个月的数据 */
   async function getList(){
        await getListApi(query.year, query.month).then((res: any) => {
			dataList.value = res.data
		}).catch(err => {
			showError(err.msg || err.errMsg || err)
		})
   }

	/** 初始化 */
	async function init() {
		if (isZero.value) {
            await getList()
			isZero.value = false
		}
	}

   
	return {
		init,  getList,
        query,  dataList
	}

})

export function useUserActiveStoreHook() {
	return useUserActiveStore(store);
}